#!/usr/bin/python
# -*- coding:utf-8 -*-

from easytrader import remoteclient
import json


class EasyTraderAPI:
    """easy trader api"""

    def __init__(self):
        self.easytrader_server_addr = None
        self.easytrader_server_port = None
        self.open_file()

    def connect_server(self):
        user = remoteclient.use(broker='ths', host=self.easytrader_server_addr,
                                port=self.easytrader_server_port)
        user.prepare(exe_path=r'C:\同花顺软件\同花顺\xiadan.exe')
        print('账户资产', user.balance)

    def open_file(self):
        with open(r'config\easy_trader_server_cfg.json') as file_object:
            config = json.load(file_object)
            self.easytrader_server_addr = config['server_addr']
            self.easytrader_server_port = config['port']

    @staticmethod
    def tmp_write_file():
        with open('server_cfg.json', mode='w') as file_object:
            x = {"a": "wo", "b": "zai", "c": "zhe", "d": "li"}
            json.dump(x, file_object, sort_keys=True, indent=4)


if __name__ == '__main__':
    # write_file()
    api = EasyTraderAPI()
    api.open_file()
    api.connect_server()
    print('exec end.')
